using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace WindowsApplication4
{
    public class Attack
    {
        Village attackingVillage;
        DataRow drAttack;
        Target target;
        Unit attackingUnit;

        public Attack(DataRow attack, Target target)
        {
            drAttack = attack;
            this.target = target;
        }

        public Unit AttackingUnit
        {
            get
            {
                if (attackingUnit == null)
                {
                    string unitName = drAttack[Compaigns.CONSTS.dtAttacks.Columns.UnitIndex].ToString();
                    attackingUnit = Worlds.currentWorld.Units[unitName];
                }
                return attackingUnit;
            }
            set
            {
                attackingUnit = value;
                drAttack[Compaigns.CONSTS.dtAttacks.Columns.UnitIndex] = value.Name;
            }
        }

        public Village TargetVillage
        {
            get
            {
                return target.TargetVillage;
            }
        }



        public string Description
        {
            get
            {
                return drAttack[Compaigns.CONSTS.dtAttacks.Columns.DescriptionIndex].ToString();
            }
            set 
            {
                drAttack[Compaigns.CONSTS.dtAttacks.Columns.DescriptionIndex] = value;
            }
        }


        public int Priority
        {
            get
            {
                return Convert.ToInt32(drAttack[Compaigns.CONSTS.dtAttacks.Columns.PriorityIndex]);
            }
            set
            {
                drAttack[Compaigns.CONSTS.dtAttacks.Columns.PriorityIndex] = value;
            }
        }


        public Village AttackingVillage
        {
            get
            {
                if (attackingVillage == null)
                {
                    attackingVillage = Worlds.currentWorld.Village(Convert.ToInt32(drAttack[Compaigns.CONSTS.dtAttacks.Columns.AttackingVillageIDIndex]));

                    if (attackingVillage == null)
                    {
                        throw new Exception("Unexpected. Cannot find village ID =" + drAttack[Compaigns.CONSTS.dtAttacks.Columns.AttackingVillageIDIndex].ToString());
                    }
                }

                return attackingVillage;
            }
        }

        internal int AttackID
        {
            get
            {
                return Convert.ToInt32(drAttack[Compaigns.CONSTS.dtAttacks.Columns.AttackIDIndex]);
            }
        }

        internal DateTime TimeLaunched
        {
            get
            {
                return (DateTime)drAttack[Compaigns.CONSTS.dtAttacks.Columns.TimeLaunchedIndex];
            }
        }

        internal bool Launched
        {
            get
            {
                return (bool)drAttack[Compaigns.CONSTS.dtAttacks.Columns.AttackLaunchedIndex];
            }
            set
            {
                drAttack[Compaigns.CONSTS.dtAttacks.Columns.AttackLaunchedIndex] = value;
            }
        }



        public TimeSpan TravelTime
        {
            get
            {
                Distance distance = AttackingVillage.DistanceTo(TargetVillage);

                return distance.TravelTime(AttackingUnit);

            }
        }

        public DateTime LandTime
        {
            get
            {
                Distance distance = AttackingVillage.DistanceTo(TargetVillage);

                TimeSpan travelTime = distance.TravelTime(AttackingUnit);

                return this.TimeLaunched.Add(travelTime);
            }
        }

        public string TravelTimeString
        {
            get
            {
                TimeSpan time = TravelTime;

                return App.FormatTWDuration(time);
                //string s = "";
                //if (time.TotalHours > 0)
                //{
                //    s += Math.Floor( time.TotalHours) + ":";
                //}
                //s += (time.Minutes < 10 ? "0" + time.Minutes.ToString() : time.Minutes.ToString()) + ":" + time.Seconds;

                //return s;

            }
        }

        public Target Target
        {
            get
            {
                return this.target;
            }
        }



        public string ToTMailString()
        {
            string s;
            s = "From [village]" + this.AttackingVillage.X + "|" + this.AttackingVillage.y + "[/village] To [village]"
                + this.TargetVillage.X + "|" + this.TargetVillage.y + "[/village] launch [i]"
                + this.AttackingUnit.Name + "[/i] " + App.FormatTWEventTime(this.TimeLaunched)
                + " arriving " + App.FormatTWEventTime(this.LandTime);                
            if (!string.IsNullOrEmpty(this.Description))
            {
                s += " ([i]" + this.Description + ")[/i]";
            }
            return s;
        }


        internal void Delete()
        {
            drAttack.Delete();
        }
    }
}
